package com.pengpeng.server.logic.user;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pengpeng.server.dao.UserRelationshipDao;
import com.pengpeng.server.entity.PengPengMessage;
import com.pengpeng.server.entity.User;
import com.pengpeng.server.entity.UserRelationship;

public class UserRelationshipRealFactory { 
     
     private Map<String,Map<String,UserRelationship>> usersrelationshipmap=null;
     private UserRelationshipDao userrelationshipdao;
     
     public UserRelationshipRealFactory()
     {
    	 usersrelationshipmap=new HashMap<String,Map<String,UserRelationship>>();
    	 usersrelationshipmap=Collections.synchronizedMap(usersrelationshipmap);
     }
     public Map<String,UserRelationship> getUserRelationship(User user)
     {
    	 return usersrelationshipmap.get(user);
     }
     public UserRelationship getUserRelationship(String myphonenum,String friendphonenum)
     {
    	 Map<String,UserRelationship>  userrelationshipmap=usersrelationshipmap.get(myphonenum);
    	 if(null==userrelationshipmap)
    	 {
    		 return userrelationshipdao.queryByKey(myphonenum, friendphonenum);
    	 }
    	 else
    	 {
    		 return userrelationshipmap.get(friendphonenum);
    	 }
    	 
     }//getUserRelationship
     
     public boolean setUserRelationship(UserRelationship userrelationship)
     {
    	 String myphonenum=userrelationship.getMyphonenum();
    	 //String ifopenmyloc=userrelationship.getIfopenmyloc();
    	 String friendphonenum=userrelationship.getFriendphonenum();
    	// String iffriendopenloc=userrelationship.getIffriendopenloc();
    	 if(true==usersrelationshipmap.containsKey(myphonenum))
		  {   
    		 Map<String,UserRelationship>  userrelationshipmap;
    		 userrelationshipmap=usersrelationshipmap.get(myphonenum);
			  if(null==userrelationshipmap)
			  {
				  userrelationshipmap=new HashMap<String,UserRelationship>();
				  userrelationshipmap=Collections.synchronizedMap(userrelationshipmap);
				  usersrelationshipmap.put(myphonenum, userrelationshipmap);
			  }
			  userrelationshipmap.put(friendphonenum, userrelationship);
		  }		
    	 return userrelationshipdao.insert(userrelationship);
    	  
     }//setUserRelationship
     
     public boolean updateUserRelationship(UserRelationship userrelationship)
     {
    	 String myphonenum=userrelationship.getMyphonenum();
    	 //String ifopenmyloc=userrelationship.getIfopenmyloc();
    	 String friendphonenum=userrelationship.getFriendphonenum();
    	// String iffriendopenloc=userrelationship.getIffriendopenloc();
    	 if(true==usersrelationshipmap.containsKey(myphonenum))
		  {   
    		 Map<String,UserRelationship>  userrelationshipmap;
    		 userrelationshipmap=usersrelationshipmap.get(myphonenum);
    		 if(null==userrelationshipmap)
			  {
				  userrelationshipmap=new HashMap<String,UserRelationship>();
				  userrelationshipmap=Collections.synchronizedMap(userrelationshipmap);
				  usersrelationshipmap.put(myphonenum, userrelationshipmap);
			  }
			  userrelationshipmap.put(friendphonenum, userrelationship);
		  }		
    	 return userrelationshipdao.update(userrelationship);
       
     }//updateUserRelationship
     
     public boolean loadUserRelationships(User user)
     {
    	 String userphonenum=user.getPhonenum();
    		
 		if(false==usersrelationshipmap.containsKey(userphonenum))
 		{
 			Map<String,UserRelationship>  userrelationshipmap;
 			userrelationshipmap=userrelationshipdao.queryByMyphonenum(userphonenum);
 			usersrelationshipmap.put(userphonenum, userrelationshipmap);
 	
 		}
    	 return true;
     }
     
     public boolean removeUserRelationship(UserRelationship userrelationship)
     {
    	 String myphonenum=userrelationship.getMyphonenum();
    	 //String ifopenmyloc=userrelationship.getIfopenmyloc();
    	 String friendphonenum=userrelationship.getFriendphonenum();
    	// String iffriendopenloc=userrelationship.getIffriendopenloc();
    	 if(true==usersrelationshipmap.containsKey(myphonenum))
		  {   
   		      Map<String,UserRelationship>  userrelationshipmap;
   		      userrelationshipmap=usersrelationshipmap.get(myphonenum);
   		 
			  if(null!=userrelationshipmap)
			  {
				  userrelationshipmap.remove(friendphonenum);
			  }
			  
		  }
   	
    	 return userrelationshipdao.delete(userrelationship);	 
     }//removeUserRelationship
     
     public UserRelationshipDao getUserrelationshipdao() {
		return userrelationshipdao;
	}//getUserrelationshipdao
     
	public void setUserrelationshipdao(UserRelationshipDao userrelationshipdao) {
		this.userrelationshipdao = userrelationshipdao;
	}//setUserrelationshipdao
	
}
